﻿using System;
using System.Threading;
using NHibernate.Event;
using NHibernate.Event.Default;
using EasyCMMS.Services.Domain;

namespace EasyCMMS.Services.Repositories
{
    public class CustomUpdateEventListener : DefaultUpdateEventListener
    {
        protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
        {
            IEntity entity = evt.Entity as IEntity;
            if (entity != null)
                ProcessEntityBeforeUpdate(entity);
            
            return base.PerformSaveOrUpdate(evt);
        }

        internal virtual void ProcessEntityBeforeUpdate(IEntity entity)
        {
            entity.ModifiedBy = Thread.CurrentPrincipal.Identity.Name;
            entity.ModifiedAt = DateTime.Now;
        }
    }
}
